package com.example.remword;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jie
 * @date 2021/4/21 -12:15
 */
@Component
public class InitWord {
    @Autowired
    private RememberWordMapper rememberWordMapper;
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");

    public void initWord() {
        List<Word> wordList = new ArrayList<>();

        // 格式：英文 + 空格 + 中文
        String str = "";

        String[] split = str.replace("\n", " ").split(" ");

        StringBuilder prior = new StringBuilder();

        for (String currentStr : split) {
            if (StringUtils.isNotBlank(currentStr) && !"\n".equals(currentStr)) {


                Matcher isNum = p.matcher(currentStr);
                if (isNum.find()) {
                    Word word = new Word(currentStr, prior.toString());
                    wordList.add(word);
                    prior = new StringBuilder();

                } else {
                    if (prior.length() == 0) {
                        // 如果是单词 先存储到 prior，
                        prior.append(currentStr);

                    } else {
                        // 如果接下来还是单词拼接 空格 + 当前字符串
                        prior.append(" ").append(currentStr);

                    }

                }
            }
        }

        rememberWordMapper.addBatch(wordList);
    }

}
